package ypmits.components.listbox {
	import flash.events.Event;
	import ypmits.components.scrollbar.ScrollBar;
	import ypmits.components.scrollbar.event.ScrollBarEvent;
	import ypmits.components.scrollbar.input.ScrollBarInput;

	import flash.display.DisplayObject;
	import flash.display.Sprite;

	/**
	 * @author Dave Lenz
	 */
	public class ScrollBarWrapper extends Sprite {

		private var scrollbar : ScrollBar;
		private var input : ScrollBarInput;
		private var scrollbarevent : ScrollBarEvent;
		private var diff : Number;

		public function ScrollBarWrapper( target : DisplayObject, height : Number ) {
			input = new ScrollBarInput( target, height);
			scrollbar = new ScrollBar( input );
			scrollbar.addEventListener( ScrollBarEvent.SCROLL_UPDATE, handlerFunction );
			addChild( scrollbar );
		}

		
		/**
		 * @param height Number
		 * @param targetheight Number
		 */
		public function update( height : Number, targetheight : Number ) : void {
			scrollbar.update( height, targetheight );
		}
		
		
		
		
		
		
		
		
		/**
		 * PRIVATES
		 */
		private function handlerFunction( e : ScrollBarEvent ) : void {
			diff = (e.toscroll.height - e.height);
			scrollbarevent = e;
			removeEventListener( Event.ENTER_FRAME, enterHandler );
			addEventListener( Event.ENTER_FRAME, enterHandler );
		}

		
		private function enterHandler(event : Event) : void {
			var newvalue : Number = -(diff * scrollbarevent.percent) + scrollbarevent.input.startyscroll;
			if( ( Math.abs( scrollbarevent.toscroll.y ) - (newvalue) ) > .001 ) {
				scrollbarevent.toscroll.y = newvalue;
			} else {
				scrollDone();
			}
		}

		
		private function scrollDone() : void {
			removeEventListener( Event.ENTER_FRAME, enterHandler );
			//input.toscroll.y = Math.round( input.toscroll.y );
		}
	}
}
